package ac.svn.accesscontrol.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.store.SVNGroup;
import ac.svn.accesscontrol.store.SVNLocation;
import ac.svn.accesscontrol.store.SVNTag;
import ac.svn.accesscontrol.store.SVNUser;

public class SVNInfoManager 
{
	private static  SVNInfoManager infoManager = new SVNInfoManager(); 
	
	private Map<String,SVNUser> usersMap;
	private Map<String,SVNGroup> groupsMap;
	private Map<String, SVNLocation> locationsMap;
	private Map<String,SVNTag> tagsMap;
	
	
	private SVNInfoManager()
	{
		this.usersMap   	 	= new HashMap<String, SVNUser>();
		this.groupsMap 	= new HashMap<String, SVNGroup>();
		this.locationsMap	= new  HashMap<String, SVNLocation>();
	}

	public static SVNInfoManager getInfoManager() {
		return infoManager;
	}

	public static void setInfoManager(SVNInfoManager infoManager) {
		SVNInfoManager.infoManager = infoManager;
	}
	
	public void updateUser(SVNUser svnUser )
	{
		infoManager.usersMap.put(svnUser.getUserAlias(), svnUser);
	}
	
	public void updateGroup(SVNGroup svnGroup)
	{
		infoManager.groupsMap.put(svnGroup.getGroupAlias(), svnGroup);
	}
	
	public void updateLocation(SVNLocation svnLocation)
	{
		infoManager.locationsMap.put(svnLocation.getLocationPath(), svnLocation);
	}
	
	public void removeGroup(String groupAlias)
	{
		infoManager.groupsMap.remove(groupAlias);
	}
	
	public void removeUser(String userAlias)
	{
		infoManager.usersMap.remove(userAlias);
	}
	
	public void removeLocation(String locationPath)
	{
		infoManager.locationsMap.remove(locationPath);
	}
	
	public Set<ISVNItem> getAllGroups()
	{
		Set<ISVNItem> allGroups = new TreeSet<ISVNItem>();
		for(SVNGroup svnGroup: infoManager.groupsMap.values())
		{
			allGroups.add(svnGroup);
		}
		return allGroups;
	}
	
	public Set<ISVNItem> getAllUsers()
	{
		Set<ISVNItem> allUsers = new TreeSet<ISVNItem>();
		for(SVNUser svnUser:infoManager.usersMap.values())
		{
			allUsers.add(svnUser);
		}
		
		return allUsers; 
	}
	
	public Set<ISVNItem> getAllLocations()
	{
		Set<ISVNItem> allLocations = new TreeSet<ISVNItem>();
		for(SVNLocation svnLocation:infoManager.locationsMap.values())
		{
			allLocations.add(svnLocation);
		}
		
		return allLocations;
	}
	
	public void addTag(SVNTag svnTag)
	{
		infoManager.tagsMap.put(svnTag.getTagId(), svnTag);
	}
	
	public SVNTag getTag(String tagId)
	{
		return infoManager.tagsMap.get(tagId);
	}
	
	public SVNGroup getGroup(String groupAlias)
	{
		return infoManager.groupsMap.get(groupAlias);
	}
	
	public SVNLocation getLocation(String locationPath)
	{
		return infoManager.locationsMap.get(locationPath);
	}
	
	public SVNUser getSvnUser(String userAlias)
	{
		return infoManager.usersMap.get(userAlias);
	}
	
	public void clearAll()
	{
		infoManager.groupsMap.clear();
		infoManager.usersMap.clear();
		infoManager.locationsMap.clear();
	//	infoManager.tagsMap.clear(); 
	}
}
